<?php

class Admin_CompanyController extends Zend_Controller_Action
{

    public function indexAction()
    {

    }

    /**
     * 行业管理
     */
    public function categoryAction()
    {
        $categoryModel = new Model_CompanyCategory();
        $this->view->categoryList = $categoryModel->dataList()->toArray();
    }

    /**
     * 添加行业
     */
    public function caddAction()
    {
        if($this->_request->isPost()){
            $categoryModel = new Model_CompanyCategory();
            $data = $this->_request->getPost();
            $categoryModel->setData($data);
            $categoryModel->save();
        }
        $this->_helper->redirector('category');

    }

    /**
     * 编辑行业
     */
    public function ceditAction()
    {
        $id = $this->_getParam('id');
        if(!$id){
            throw new Exception("请指定要编辑的行业！");
        }
        $categoryModel = new Model_CompanyCategory();
        $category = $categoryModel->find($id);
        if($category){
            if($this->_request->isPost()){
                $category->setData($this->_getAllParams());
                $category->save();
                $this->_helper->redirector('category');
            }else{
                $this->view->category = $category;
            }
        }else{
            throw new Exception("您要编辑的行业不存在");
        }
    }

    /**
     * 删除单个或多个行业，同时删除该类别下的所有资讯
     */
    public function cdeleteAction()
    {
        $id = $this->_getParam('id');
        if(empty($id)){
            throw new Exception("请指定要删除的行业！");
        }
        $categoryModel = new Model_CompanyCategory();
        $companyModel = new Model_User();
        if(is_array($id)){
            $categoryModel->getTable()->delete(array('id IN (?)'=>$id));
            $companyModel->getTable()->update(array('company_category'=>0),array('company_category IN (?)'=>$id));
        }else{
            $category = $categoryModel->find($id);
            if($category){
                $categoryModel->getTable()->delete(array('id=?'=>$id));
                $companyModel->getTable()->update(array('company_category'=>0),array('company_category=?'=>$id));
            }else{
                throw new Exception("您要删除的行业不存在");
            }
        }
        $this->_helper->redirector('category');
    }
}